//
//  THCommentViewController.swift
//  LaiLang
//
//  Created by David on 2017/2/20.
//  Copyright © 2017年 David. All rights reserved.
//

import UIKit

class THCommentViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UITextViewDelegate {

    var tableView: UITableView!
    
    var commentData: NSArray?
    var dataModal: NSDictionary?
    
    var commentInputView: UIView!
    var commentInput: LLTextView!
    
    convenience init(dataModal: NSDictionary){
        self.init()
        self.dataModal = dataModal
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        let navbar = LLAdaptHideNavBar(frame: CGRect(x: 0, y: 0, width: view.bounds.width, height: 64), title: "详情", navigator: navigationController)
        
        tableView = UITableView(frame: CGRect(x: 0, y: 44, width: view.bounds.width, height: view.bounds.height-44))
        tableView.delegate = self
        tableView.dataSource = self
//        tableView.separatorStyle = .none
        tableView.keyboardDismissMode = .onDrag
        tableView.backgroundColor = UIColor.groupTableViewBackground
        tableView.tableFooterView = UIView(frame: CGRect.zero)
        view.addSubview(tableView)
        
        commentInputView = UIView(frame: CGRect(x: 0, y: view.bounds.height - 44, width: view.bounds.width, height: 44))
        commentInputView.backgroundColor = UIColor.groupTableViewBackground
        view.addSubview(commentInputView)
        
        
        commentInput = LLTextView(frame: CGRect(x: 16, y: 4, width: view.bounds.width - 32, height: 36))
        commentInput.placeholder = "评论"
        commentInput.maxLength = THMaxContentLength
        commentInput.returnKeyType = .send
        commentInput.weakDelegate = self
        commentInputView.addSubview(commentInput)
        
        
        view.addSubview(navbar)
        
        NotificationCenter.default.addObserver(self, selector: #selector(THCommentViewController.keyboardShow(noti:)), name: Notification.Name.UIKeyboardWillShow, object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(THCommentViewController.keyboardHide(noti:)), name: Notification.Name.UIKeyboardWillHide, object: nil)
        
    }

    func keyboardShow(noti: Notification){
        let info = noti.userInfo!
        let heightValue = info[UIKeyboardFrameBeginUserInfoKey] as! NSValue
        let height = heightValue.cgRectValue.height
        var time:TimeInterval = 0
        let timeValue = info[UIKeyboardAnimationDurationUserInfoKey] as! NSValue
        timeValue.getValue(&time)
        
        UIView.animate(withDuration: time) { 
            self.commentInputView.center.y = self.commentInputView.center.y - height
        }
    }
    
    func keyboardHide(noti: Notification){
        let info = noti.userInfo!
        
        var time:TimeInterval = 0
        let timeValue = info[UIKeyboardAnimationDurationUserInfoKey] as! NSValue
        timeValue.getValue(&time)
        
        UIView.animate(withDuration: time) {
            self.commentInputView.center.y = self.view.bounds.height - self.commentInputView.bounds.height/2
        }
    }
    
    func textViewDidChange(_ textView: UITextView) {
        // TODO: 动态高度
    }
    
    override func viewDidDisappear(_ animated: Bool) {
        super.viewDidDisappear(animated)
        NotificationCenter.default.removeObserver(self, name: Notification.Name.UIKeyboardWillShow, object: nil)
        NotificationCenter.default.removeObserver(self, name: Notification.Name.UIKeyboardWillHide, object: nil)
    }
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 2
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if section == 1{
            if commentData != nil{
                return commentData!.count > 0 ? commentData!.count : 1
            }
        }
        return 1
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
//        if indexPath.section == 0{
//            if indexPath.row == 0{
//                return 256
//            }
//        }else if indexPath.section == 1{
//            return 108
//        }
        return 108
    }
    
    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        if section == 0{
            return "热门评论"
        }
        if section == 1{
            return "所有评论"
        }
        
        return nil
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = UITableViewCell(style: .default, reuseIdentifier: "item")
        cell.selectionStyle = .none
//        cell.backgroundColor = UIColor.clear
        if indexPath.section == 0{
            let avatar = UIButton(frame: CGRect(x: 16, y: 8, width: 36, height: 36))
            avatar.backgroundColor = UIColor.lightGray
            avatar.layer.cornerRadius = 18
            cell.addSubview(avatar)
            
            let nameLabel = UILabel(frame: CGRect(x: 60, y: 8, width: view.bounds.width - 76, height: 20))
            cell.addSubview(nameLabel)
            
            let contentText = UITextView(frame: CGRect(x: 60, y: 28, width: view.bounds.width - 76, height: 52))
            contentText.font = UIFont.systemFont(ofSize: 14)
            contentText.isEditable = false
            contentText.isSelectable = false
            contentText.isScrollEnabled = false
            contentText.backgroundColor = UIColor.clear
            cell.addSubview(contentText)
            
            
            let likeBtn = UIButton(frame: CGRect(x: 60, y: 80, width: 64, height: 20))
            cell.addSubview(likeBtn)
            
            
            let showStr = "\u{e6a4} 999+"
            let attrStr = NSMutableAttributedString(string: showStr)
            attrStr.addAttribute(NSForegroundColorAttributeName, value: UIColor.lightGray, range: NSMakeRange(0, showStr.characters.count))
            attrStr.addAttribute(NSFontAttributeName, value: UIFont(name: "iconfont", size: 16)!, range: NSMakeRange(0, 1))
            attrStr.addAttribute(NSFontAttributeName, value: UIFont.systemFont(ofSize: 14), range: NSMakeRange(1, showStr.characters.count - 1))
            likeBtn.setAttributedTitle(attrStr, for: .normal)
            
            let replyBtn = UIButton(frame: CGRect(x: 128, y: 80, width: 48, height: 20))
            replyBtn.setTitle("回复", for: .normal)
            replyBtn.setTitleColor(UIColor.lightGray, for: .normal)
            replyBtn.titleLabel?.font = UIFont.systemFont(ofSize: 14)
            cell.addSubview(replyBtn)
            
            let timeLabel = UILabel(frame: CGRect(x: 178 , y: 80, width: 80, height: 20))
            timeLabel.textColor = UIColor.lightGray
            timeLabel.font = UIFont.systemFont(ofSize: 14)
            cell.addSubview(timeLabel)
            
            
            nameLabel.text = "yonghu 回复 xxxx"
            contentText.text = "comment"
            
            timeLabel.text = "刚刚"
            
        }else if indexPath.section == 1{
            var empty = true
            
            if commentData != nil{
                if commentData!.count > 0{
                    empty = false
                    // TODO: 评论item
                    let avatar = UIButton(frame: CGRect(x: 16, y: 8, width: 36, height: 36))
                    avatar.backgroundColor = UIColor.lightGray
                    avatar.layer.cornerRadius = 18
                    cell.addSubview(avatar)
                    
                    let nameLabel = UILabel(frame: CGRect(x: 60, y: 8, width: view.bounds.width - 76, height: 20))
                    cell.addSubview(nameLabel)
                    
                    let contentText = UITextView(frame: CGRect(x: 60, y: 36, width: view.bounds.width - 76, height: 20))
                    contentText.font = UIFont.systemFont(ofSize: 14)
                    contentText.isEditable = false
                    contentText.isSelectable = false
                    contentText.isScrollEnabled = false
                    cell.addSubview(contentText)
                    
                    let likeBtn = UIButton(frame: CGRect(x: 16, y: 80, width: 48, height: 20))
                    cell.addSubview(likeBtn)
                    
                    
                    let showStr = "\u{e6a4} 0"
                    let attrStr = NSMutableAttributedString(string: showStr)
                    attrStr.addAttribute(NSForegroundColorAttributeName, value: UIColor.lightGray, range: NSMakeRange(0, 3))
                    attrStr.addAttribute(NSFontAttributeName, value: UIFont(name: "iconfont", size: 16)!, range: NSMakeRange(0, 1))
                    attrStr.addAttribute(NSFontAttributeName, value: UIFont.systemFont(ofSize: 14), range: NSMakeRange(1, showStr.characters.count - 1))
                    likeBtn.setAttributedTitle(attrStr, for: .normal)
                    
                    let replyBtn = UIButton(frame: CGRect(x: 80, y: 80, width: 48, height: 20))
                    replyBtn.setTitle("回复", for: .normal)
                    replyBtn.setTitleColor(UIColor.lightGray, for: .normal)
                    cell.addSubview(replyBtn)
                    
                    let timeLabel = UILabel(frame: CGRect(x: 144, y: 80, width: 80, height: 20))
                    timeLabel.textColor = UIColor.lightGray
                    cell.addSubview(replyBtn)
                    
                    
                    nameLabel.text = "yonghu"
                    contentText.text = "comment"
                    
                    timeLabel.text = "刚刚"
                }
            }
            
            if empty{
//                let emptyView = showNothingPlaceholder(frame: CGRect(x: 16, y: 36, width: view.bounds.width - 32, height: 20))
//                cell.addSubview(emptyView)
                
                let avatar = UIButton(frame: CGRect(x: 16, y: 8, width: 36, height: 36))
                avatar.backgroundColor = UIColor.lightGray
                avatar.layer.cornerRadius = 18
                cell.addSubview(avatar)
                
                let nameLabel = UILabel(frame: CGRect(x: 60, y: 8, width: view.bounds.width - 76, height: 20))
                cell.addSubview(nameLabel)
                
                let contentText = UITextView(frame: CGRect(x: 60, y: 28, width: view.bounds.width - 76, height: 52))
                contentText.font = UIFont.systemFont(ofSize: 14)
                contentText.isEditable = false
                contentText.isSelectable = false
                contentText.isScrollEnabled = false
                contentText.backgroundColor = UIColor.clear
                cell.addSubview(contentText)
                
                
                let likeBtn = UIButton(frame: CGRect(x: 60, y: 80, width: 64, height: 20))
                cell.addSubview(likeBtn)
                
                
                let showStr = "\u{e6a4} 999+"
                let attrStr = NSMutableAttributedString(string: showStr)
                attrStr.addAttribute(NSForegroundColorAttributeName, value: UIColor.lightGray, range: NSMakeRange(0, showStr.characters.count))
                attrStr.addAttribute(NSFontAttributeName, value: UIFont(name: "iconfont", size: 16)!, range: NSMakeRange(0, 1))
                attrStr.addAttribute(NSFontAttributeName, value: UIFont.systemFont(ofSize: 14), range: NSMakeRange(1, showStr.characters.count - 1))
                likeBtn.setAttributedTitle(attrStr, for: .normal)
                
                let replyBtn = UIButton(frame: CGRect(x: 128, y: 80, width: 48, height: 20))
                replyBtn.setTitle("回复", for: .normal)
                replyBtn.setTitleColor(UIColor.lightGray, for: .normal)
                replyBtn.titleLabel?.font = UIFont.systemFont(ofSize: 14)
                cell.addSubview(replyBtn)
                
                let timeLabel = UILabel(frame: CGRect(x: 178 , y: 80, width: 80, height: 20))
                timeLabel.textColor = UIColor.lightGray
                timeLabel.font = UIFont.systemFont(ofSize: 14)
                cell.addSubview(timeLabel)
                
                
                nameLabel.text = "yonghu 回复 xxxx"
                contentText.text = "comment"
                
                timeLabel.text = "刚刚"
            }
        }
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if indexPath.section == 1{
            tableView.deselectRow(at: indexPath, animated: true)
            commentInput.becomeFirstResponder()
        }
    }
    
    func hitLikeBtn(sender: UIButton){
        print("like")
    }
    
    func gotoComment(sender: UIButton){
        print("going to comment")
        commentInput.becomeFirstResponder()
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    func showNothingPlaceholder(frame: CGRect) ->UIView{
        let label = UILabel(frame: CGRect(x: 16, y: 0, width: frame.width - 32, height: frame.height))
        label.textAlignment = .center
        label.textColor = UIColor.lightGray
        label.font = UIFont.systemFont(ofSize: 14)
        label.text = emptyPlaceholder
        return label
    }
    
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
